package me.zhengjie.modules.web.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.zhengjie.config.entity.PageData;
import me.zhengjie.config.entity.ResponseBaseEntity;
import me.zhengjie.config.enums.AtmrErrorEnum;
import me.zhengjie.config.exception.WeChatException;
import me.zhengjie.modules.web.domain.order.CommonPageReq;
import me.zhengjie.modules.web.domain.order.OrderListRequest;
import me.zhengjie.modules.web.domain.order.OrderListResponse;
import me.zhengjie.modules.web.domain.order.WholeBaseResponse;
import me.zhengjie.modules.web.domain.whole.WholeClassResponse;
import me.zhengjie.modules.web.domain.whole.WholeCourseResponse;
import me.zhengjie.modules.web.domain.whole.WholeReportResponse;
import me.zhengjie.modules.web.service.order.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController
@RequestMapping("web/whole")
@Api(tags = "前端接口-查看全部")
public class WholeController {


    @Autowired
    private IOrderService orderService;

    @PostMapping("order")
    @ApiOperation(value = "WEB-全部订单")
    public ResponseBaseEntity order(@RequestBody OrderListRequest requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (Objects.isNull(requestEntity.getOrderType())){
                throw new WeChatException("缺少参数");
            }
            if (requestEntity.getSize() > 20){
                requestEntity.setSize(20);
            }
            PageData<OrderListResponse> result = orderService.list(requestEntity);
            response.setBody(result);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
    @PostMapping("course")
    @ApiOperation(value = "WEB-全部课程")
    public ResponseBaseEntity course(@RequestBody CommonPageReq requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (requestEntity.getSize() > 20){
                requestEntity.setSize(20);
            }
            PageData<WholeCourseResponse> result = orderService.course(requestEntity);
            response.setBody(result);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }

    @PostMapping("classes")
    @ApiOperation(value = "WEB-全部班级")
    public ResponseBaseEntity classes(@RequestBody CommonPageReq requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (requestEntity.getSize() > 20){
                requestEntity.setSize(20);
            }
            PageData<WholeClassResponse> result = orderService.classes(requestEntity);
            response.setBody(result);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
    @PostMapping("report")
    @ApiOperation(value = "WEB-全部报告")
    public ResponseBaseEntity report(@RequestBody CommonPageReq requestEntity){
        ResponseBaseEntity response = new ResponseBaseEntity();
        try {
            if (requestEntity.getSize() > 20){
                requestEntity.setSize(20);
            }
            PageData<WholeReportResponse> result = orderService.report(requestEntity);
            response.setBody(result);
        }catch (WeChatException e){
            response.getHeader().setErrCode("-1");
            response.getHeader().setErrMsg(e.getMsg());
        }catch (Exception e){
            response.getHeader().setErrCode(AtmrErrorEnum.EC_ALL_99999.getErrorCode());
            response.getHeader().setErrMsg(AtmrErrorEnum.EC_ALL_99999.getErrorMessage());
        }
        return response;
    }
}